<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" :value="inputValue" @change="handleInputChange"/>
    <a-button type="primary" @click="addItemToList">添加事项</a-button>

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="cbStatusChanged(item.id, $event)">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="removeItemById(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{unDoneLength}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="currentkey === 'All' ? 'primary' : 'default'" @click="currentBtn('All')">全部</a-button>
          <a-button :type="currentkey === 'Undone' ? 'primary' : 'default'" @click="currentBtn('Undone')">未完成</a-button>
          <a-button :type="currentkey === 'Done' ? 'primary' : 'default'" @click="currentBtn('Done')">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clean">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'

export default {
  name: 'App',
  data () {
    return {
    }
  },
  created () {
    // 获取最新的列表信息
    this.$store.dispatch('getList')
  },
  computed: {
    ...mapState(['list', 'inputValue', 'currentkey']),
    ...mapGetters(['unDoneLength', 'infoList'])
  },
  methods: {
    // 当 input 栏数据的双向绑定
    handleInputChange (e) {
      this.$store.commit('setInputeValue', e.target.value)
    },
    // 添加 项 到列表中
    addItemToList () {
      if (this.inputValue.trim().length <= 0) {
        return this.$message.warning('文本框内容不能为空')
      }
      this.$store.commit('addItem')
    },
    // 删除一个指定 ID 的项
    removeItemById (id) {
      this.$store.commit('removeItem', id)
    },
    // 每一项的 done 进行双向绑定
    cbStatusChanged (id, evnet) {
      const param = {
        id: id,
        done: event.target.checked
      }
      this.$store.commit('statusChanged', param)
    },
    // 清除已完成
    clean () {
      this.$store.commit('cleanDone')
    },
    // 当前选中的按钮
    currentBtn (key) {
      this.$store.commit('currentButton', key)
    }
  }
}
</script>

<style lang="less" scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
